import { _decorator, Button, Component, Node } from 'cc';
import { RedDotTree } from '../../thirdParty/redDotTree/RedDotTree';
import { Dot } from './Dot';
import { RedNumTree } from '../../thirdParty/RedNumTree/RedNumTree';
const { ccclass, property } = _decorator;

@ccclass('DotTest')
export class DotTest extends Component {

    @property(Dot)
    dotRoot: Dot = null!;

    @property(Dot)
    dot1: Dot = null!;

    @property(Dot)
    dot2: Dot = null!;

    @property(Dot)
    dot3: Dot = null!;

    @property(Dot)
    dot4: Dot = null!;

    @property(Dot)
    dot5: Dot = null!;

    redDotTree: RedNumTree = null!;
    onLoad() {
        this.redDotTree = new RedNumTree();
    }

    start() {
        // 添加根节点
        this.redDotTree.addNode("button");
        // 添加叶子节点
        this.redDotTree.addNode("button/btn1");
        this.redDotTree.addNode("button/btn2");
        this.redDotTree.addNode("button/btn3");

        this.redDotTree.addNode("button/btn3/btn4");
        this.redDotTree.addNode("button/btn3/btn5");

        //注册事件
        this.redDotTree.registerCallback("button", this.onRootChanged.bind(this));
        this.redDotTree.registerCallback("button/btn1", this.onBtn1RedDotChanged.bind(this));
        this.redDotTree.registerCallback("button/btn2", this.onBtn2RedDotChanged.bind(this));
        this.redDotTree.registerCallback("button/btn3", this.onBtn3RedDotChanged.bind(this));

        this.redDotTree.registerCallback("button/btn3/btn4", this.onBtn4RedDotChanged.bind(this));
        this.redDotTree.registerCallback("button/btn3/btn5", this.onBtn5RedDotChanged.bind(this));


        //设置值
        this.redDotTree.changeRedDotState("button/btn1", 10);
        this.redDotTree.changeRedDotState("button/btn2", 5);

        this.redDotTree.changeRedDotState("button/btn4", 2);
        this.redDotTree.changeRedDotState("button/btn5", 2);

    }

    onRootChanged(redNum: number) {
        console.log("onRootChanged", redNum);
        this.dotRoot.setDot(redNum);

    }

    onBtn1RedDotChanged(redNum: number) {
        console.log("onBtn1RedDotChanged1", redNum);
        this.dot1.setDot(redNum);
    }

    onBtn2RedDotChanged(redNum: number) {
        console.log("onBtn2RedDotChanged2", redNum);
        this.dot2.setDot(redNum);
    }

    onBtn3RedDotChanged(redNum: number) {
        console.log("onBtn3RedDotChanged3", redNum);
        this.dot3.setDot(redNum);
    }

    onBtn4RedDotChanged(redNum: number) {
        console.log("onBtn3RedDotChanged4", redNum);
        this.dot4.setDot(redNum);
    }

    onBtn5RedDotChanged(redNum: number) {
        console.log("onBtn3RedDotChanged5", redNum);
        this.dot5.setDot(redNum);
    }



    onBtn1Click() {
        console.log("onBtn1Click1");
        let b = !(this.redDotTree.getRedDotNum("button/btn1") > 0);
        this.redDotTree.changeRedDotState("button/btn1", rand());
    }

    onBtn2Click() {
        console.log("onBtn1Click2");
        this.redDotTree.changeRedDotState("button/btn2", rand());
    }

    onBtn4Click() {
        console.log("onBtn1Click4");
        this.redDotTree.changeRedDotState("button/btn3/btn4", rand());
    }

    onBtn5Click() {
        console.log("onBtn5Click");
        this.redDotTree.changeRedDotNum("button/btn3/btn5", rand());
    }

    update(deltaTime: number) {

    }
}


function rand() {
    return Math.random() * 10 | 0
}